﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq.Mapping;
using System.Collections.ObjectModel;

namespace LinqToSQL
{
  class Program
  {
    static void Main(string[] args)
    {
      IList<Line> lines = SelectAll<Line>();
      foreach (Line l in lines)
      {
        Console.WriteLine(l.tag);
      }
      Console.ReadLine();
    }

    //public static T Get<T>(Dictionary<string, object> queryProperties) where T : class
    //{
    //  try
    //  {
    //    using (ModelDataContext context = new ModelDataContext())
    //    {
    //      // get the table by the type passed in
    //      var table = context.GetTable<T>();
    //      // get the metamodel mappings (database to // domain objects)
    //      MetaModel modelMap = table.Context.Mapping;
    //      // get the data members for this type
    //      ReadOnlyCollection<MetaDataMember> dataMembers = modelMap.GetMetaType(typeof(T)).DataMembers;
    //      // find the primary key field name
    //      // by checking for IsPrimaryKey
    //      foreach (KeyValuePair<string, object> keyValuePair in queryProperties)
    //      {

    //      }
    //      string pk = (dataMembers.Single<MetaDataMember>(m => m.IsPrimaryKey)).Name;

    //      // return a single object where the id argument 
    //      // matches the primary key field value
    //      return table.SingleOrDefault<T>(delegate(T t)
    //      {
            
    //        String memberId = t.GetType().GetProperty(pk).GetValue(t, null).ToString();
    //        return memberId.ToString() == id.ToString();

    //      });
    //    }
    //  }
    //  catch (Exception)
    //  {
    //    throw;
    //  }
    //}

    /// <summary>
    /// Select All by Type
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <returns>Matching table as typed list</returns>
    public static IList<T> SelectAll<T>() where T : class
    {
      try
      {
        using (ModelDataContext context = new ModelDataContext())
        {
          // get the table matching the type
          // and return all of it as typed list
          var table = context.GetTable<T>().ToList<T>();
          return table;
        }
      }
      catch (Exception)
      {
        throw;
      }
    }
  }
}
